﻿#include <iostream>
#include <list>
#include <string>


void* myAlloc(size_t size)
{
	return malloc(size);
}

void myFree(void * ptr)
{
	return free(ptr);
}

void * operator new(size_t size)
{	std::cout << __FUNCTION__ << std::endl;	return myAlloc(size);}

void *operator new[](size_t size)
{	std::cout << __FUNCTION__ << std::endl;	return myAlloc(size);}

void operator delete(void* ptr)
{	std::cout << __FUNCTION__ << std::endl;	myFree(ptr); }

void operator delete[](void* ptr)
{	std::cout << __FUNCTION__ << std::endl;	myFree(ptr); }

class A {
public:
	virtual void vfunA() {
		std::cout << __FUNCTION__ << std::endl;
	}
private:
	int m_data1, m_data2;
};

class B :public A
{
public:
	virtual void vfunA() {
		std::cout << __FUNCTION__ << std::endl;
	}
private:
	int m_data3;
};

class C :public A
{
public:
	virtual void vfunA() {
		std::cout << __FUNCTION__ << std::endl;
	}
private:
	int m_data3;
};

void foo() { std::cout << __FUNCTION__ << __LINE__ << std::endl; }

class D :public A
{
public:

	void* operator new(size_t size);
	virtual void vfunA() {
		std::cout << __FUNCTION__ << std::endl;
	}

	void foo() { std::cout << __FUNCTION__ << __LINE__ << std::endl; }
private:
	int m_data3;
};

void* D::operator new(size_t size)
{
	std::cout << __FUNCTION__  << __LINE__ << std::endl;
	return malloc(size);
}



int main(void)
{
	std::string;
	std::list<A *> listA;
	B b;
	C c;
	D d;
	d.foo();
	::foo();

	D * pd = new D;
	delete pd;

	listA.push_back(&b);
	listA.push_back(&c);
	listA.push_back(&d);

	for (auto i : listA)
	{
		i->vfunA();//每个类都是自己的特殊实现
	}
}
